package com.heatup.apt.common.dao.redis;

import com.heatup.apt.common.dao.user.UserInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.heatup.apt.model.user.UserInfo;

@Component("userInfoRedisDao")
public class UserInfoRedisDao extends BaseRedisDao<UserInfo> {
	private Logger logger = LoggerFactory.getLogger(UserInfoRedisDao.class);
	private final static String USER_INFO_REDIS_KEY = "USER_INFO:";
	@Autowired
	private UserInfoMapper userInfoMapper;

	/**
	 * 从Redis查询UserInfo,如不存在，则从数据库中获取。
	 * @return
	 */
	public UserInfo queryUserInfoByOpenIdForm(String openId) {
		UserInfo userInfo = null;
		final String key = USER_INFO_REDIS_KEY + openId;
		userInfo = this.get(key, UserInfo.class);
		if (userInfo == null) {
			userInfo = userInfoMapper.selectByOpenid(openId);
		}
		return userInfo;
	}

	/**
	 * 获取UserInfo信息存入redis
	 * @param userInfo
	 */
	public void addUserInfoToRedis(UserInfo userInfo) {
		if (userInfo != null && userInfo.getOpenid() != null) {
			String key = USER_INFO_REDIS_KEY + userInfo.getOpenid();
			this.save(key, userInfo);
		}
	}

}
